//
//  PaintingViewController.swift
//  NDAi
//
//  Created by 丸子 on 2024/6/3.
//

import UIKit
import JXSegmentedView
class PaintingViewController: BaseViewController, JXSegmentedListContainerViewListDelegate {
    let disposeBag = DisposeBag()
    let viewModel = PaintingViewModel()
    
    var isOpen:Bool = false {
        didSet {
            self.tableView.reloadData()
        }
    }
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(cellWithClass: PaintingContentCell.self)
        tableView.register(cellWithClass: PaintingStyleCell.self)
        tableView.register(cellWithClass: PaintingScaleCell.self)
        tableView.rowHeight = UITableView.automaticDimension
        tableView.separatorStyle = .none
        tableView.backgroundColor = .clear
        tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 66, right: 0)
        return tableView
    }()
    
    private lazy var paintBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = Theme.color.main
        button.setTitle("立即绘画", for: .normal)
        button.titleLabel?.font = UIFont.pingFangSCRegular(ofSize: 16)
        button.allRound(24)
        return button
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.viewModel.getImageDataList()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(hexString: "#F3F2F7")

        self.view.addSubview(tableView)
        self.view.addSubview(paintBtn)
        tableView.snp.makeConstraints { make in
            make.top.equalTo(self.safeTop)
            make.leading.trailing.equalToSuperview()
            make.bottom.equalTo(self.safeBottom)
        }
        paintBtn.snp.makeConstraints { make in
            make.bottom.equalTo(self.safeBottom).offset(-8)
            make.leading.trailing.equalToSuperview().inset(16)
            make.height.equalTo(48)
        }
        
        paintBtn.setClosureTouchUpInside_k { [weak self] in
            self?.create()
        }
        bindEvent()
    }
    
    func bindEvent(){
        viewModel.reloadDataSubject.subscribe {[weak self] _ in
            self?.tableView.reloadData()
        }.disposed(by: disposeBag)
        
        viewModel.createSuccessSubject.subscribe {[weak self] id in
            let vc = PaintingGenerateViewController()
            vc.checkId = id
            self?.navigationController?.pushViewController(vc)
        }.disposed(by: disposeBag)
    }
    
    func create(){
        if !Appearance.shared.isLoign {
            GlobalRx.loginSubject.onNext(())
            return
        }
        viewModel.imageCreate(isOpen: self.isOpen)
    }
 
    
    func listView() -> UIView {
        return self.view
    }
}
extension PaintingViewController: UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.item == 0 {
            let cell = tableView.dequeueReusableCell(withClass: PaintingContentCell.self)
//            cell.switchBtn.setClosureTouchUpInside_k { [weak self] in
//                guard let self = self else { return }
//                self.isOpen = !self.isOpen
//                cell.switchBtn.isSelected = self.isOpen
//                cell.twoDescView.isHidden = !self.isOpen
//                cell.twoDescTextView.isHidden = !self.isOpen
//                cell.twoCountLb.isHidden = !self.isOpen
//            }
            cell.descTextView.text = self.viewModel.message ?? ""
//            cell.twoDescTextView.text = self.viewModel.negativePrompt
            cell.chooseDesc = { [weak self] text in
                self?.viewModel.message = text
            }
            cell.chooseOther = { [weak self] text in
                self?.viewModel.negativePrompt = text
            }
            return cell
        }else if indexPath.item == 1 {
            let cell = tableView.dequeueReusableCell(withClass: PaintingStyleCell.self)
            cell.setup(model: viewModel.listModel?.style)
            cell.chooseStyleBag = {[weak self] id in
                self?.viewModel.style_id = id
            }
            return cell
        }else {
            let cell = tableView.dequeueReusableCell(withClass: PaintingScaleCell.self)
            cell.setup(model: viewModel.listModel?.proportion)
            cell.choosePBag = {[weak self] id in
                self?.viewModel.proportion_id = id
            }
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.item == 0 {
//            let def = 239.0
            let def = 179.0
            let two = 84.0
            return isOpen ? def + two : def
        }else if indexPath.item == 1{
            var height = (SCREEN_WIDTH - 56) / 4
            height = height * 2 + 10
            return height + 46
        }else {
            let height = (SCREEN_WIDTH - 56) / 4
            return height + 66
        }
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        kHideKeyboard()
    }
}
